//
//  MetaDataTool.m
//  团购
//
//  Created by 樊荣海 on 15/1/22.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//  元数据管理类
//  1.城市数据
//  2.分区数据
//  3.分类


#import "MetaDataTool.h"

@implementation MetaDataTool {
    
    NSMutableArray *_visitedCityNames; // 存储曾经访问过城市的名称
    
    NSMutableDictionary *_totalCities; // 存放所有的城市 key 是城市名  value 是城市对象
    //
    CitySectionModel *_visitedSection; // 最近访问的城市组
    
    NSMutableArray *_temp;
    
    NSDictionary *_quanpinDict;//全拼字典
    NSDictionary *_jianpinDict;//简拼字典
}

singleton_implementation(MetaDataTool)


- (instancetype)init {
    
    if (self == [super init]) {
        
        _temp = [NSMutableArray array];
        
        //1.初始化 城市元素数据
        [self initCityData];
        
        //2.初始化 属性元素数据
        [self initCategoryData];
        
        //3.初始化 商品元素数据
        [self initOrderData];
        
        //4.初始化 城市拼音数据
        [self initPinyinData];
    }
    
    return self;
    
}

- (void)initPinyinData {
    
    //    NSDictionary *citiesDict = [[MVdataHandle shareInstance] allCity];
    
    //全拼字典
    NSMutableDictionary *quanpinDict = [NSMutableDictionary dictionary];
    NSMutableDictionary *jianpinDict = [NSMutableDictionary dictionary];
    
    for (NSString *key in _totalCities) {
        
        NSString *pinyin = [[PinyinTool sharedPinyinTool] pinyinTrans:key];
        
        NSMutableString *firstWord = [NSMutableString string];
        NSArray *array = [pinyin componentsSeparatedByString:@" "];
        for (NSString *str in array) {
            [firstWord appendString:[str substringToIndex:1]];
        }
        
        //去掉空格的拼音
        NSString *pinyinNew = [pinyin stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        [quanpinDict setObject:_totalCities[key] forKey:pinyinNew.uppercaseString];
        [jianpinDict setObject:_totalCities[key] forKey:firstWord.uppercaseString];
        
    }
    
    _quanpinDict = quanpinDict;
    _jianpinDict = jianpinDict;
}

//初始化商品数据
- (void)initOrderData {
    
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Orders" ofType:@"plist"]];
    
    NSMutableArray *temp = [NSMutableArray array];
    
    NSInteger count = array.count;
    
    for (int i = 0; i<count; i++){
        OrderModel
        *model = [[OrderModel alloc] init];
        model.name = array[i];
        model.index = i + 1;
        [temp addObject:model];
    }
    
    _totalOrders = temp;
    
}

- (void)initCategoryData {
    
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Categories" ofType:@"plist"]];
    
    NSMutableArray *tempArray = [NSMutableArray array];
    
    for (NSDictionary *dict in array) {
        
        CateModel *model = [[CateModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        
        [tempArray addObject:model];
    }
    
    _totalCategories = tempArray;
    
}

- (void)initCityData {
    
    
    _visitedSection = [[CitySectionModel alloc] init];
    
    _visitedSection.name = @"最近访问";
    _visitedSection.cities = [NSMutableArray array];
    
    //初始化项目中的所有元数据
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Cities" ofType:@"plist"]];
    
    NSMutableArray *hotCities = [NSMutableArray array];
    // 存放所有的城市
    _totalCities = [NSMutableDictionary dictionary];
    
    //1.热门城市--在下面添加数据
    CitySectionModel *hotSectionModel = [[CitySectionModel alloc] init];
    hotSectionModel.name = @"热门城市";
    
    [_temp addObject:hotSectionModel];
    
    //2.A-Z
    for (NSDictionary *dict in array) {
        CitySectionModel *sectionModel = [[CitySectionModel alloc] init];
        [sectionModel setValuesForKeysWithDictionary:dict];
        
        [_temp addObject:sectionModel];
        
        //增加热门citymodel
        for (CityModel *city in sectionModel.cities) {
            if (city.hot) {
                [hotCities addObject:city];
            }
            
            [_totalCities setObject:city forKey:city.name];
            
        }
        
    }
    
    hotSectionModel.cities = hotCities;
    
    //3.最近访问
    [self updateVisitedCity];
    
}

- (void)updateVisitedCity {
    
    if (_visitedCityNames == nil) {
        _visitedCityNames = [NSMutableArray array];
    }
    
    //先查询数据库
    [_visitedCityNames removeAllObjects];
    _visitedCityNames = [[DBTool sharedDBTool] selectAllCityModel];
    
    [_visitedSection.cities removeAllObjects];
    
    for (NSString *name in _visitedCityNames) {
        CityModel *city = _totalCities[name];
        [_visitedSection.cities addObject:city];
    }
    
    if (_visitedCityNames.count != 0) {
        
        if (![_temp containsObject:_visitedSection]) {
            [_temp insertObject:_visitedSection atIndex:0];
        } else {
            [_temp replaceObjectAtIndex:0 withObject:_visitedSection];
        }
        
    }
    
    //重新赋值
    _totalCitySections = _temp;
    
#warning BUG
    [self initOrderData];
}

- (NSArray *)citymodelsWithString:(NSString *)string {
    
    
    //A-Z所有城市名转化为拼音
    //            NSDictionary *citiesDict = [[MetaDataTool sharedMetaDataTool] totalCities];
    
    NSMutableArray *citymodels = [NSMutableArray array];
    
    [_totalCities enumerateKeysAndObjectsUsingBlock:^(NSString *key, CityModel *model, BOOL *stop) {
        
        NSString *pinyin = [[PinyinTool sharedPinyinTool] pinyinTrans:model.name];
        
        NSMutableString *firstWord = [NSMutableString string];
        NSArray *array = [pinyin componentsSeparatedByString:@" "];
        for (NSString *str in array) {
            [firstWord appendString:[str substringToIndex:1]];
        }
        
        //去掉空格的拼音
        NSString *pinyinNew = [pinyin stringByReplacingOccurrencesOfString:@" " withString:@""];
        
        if ([model.name rangeOfString:string].length != 0 ||
            [pinyinNew.uppercaseString rangeOfString:string.uppercaseString].length != 0||
            [firstWord.uppercaseString rangeOfString:string.uppercaseString].length != 0) {
            
            [citymodels addObject:model];
        }
        
        
    }];
    
    return citymodels;
    
}

- (NSArray *)cityModalsWithString:(NSString *)string {
    
    NSMutableArray *cityModals = [NSMutableArray array];
    
    for (NSString *key in _quanpinDict) {
        
        if ([key.uppercaseString rangeOfString:string.uppercaseString].length != 0) {
            
            if (![cityModals containsObject:_quanpinDict[key]]) {
                [cityModals addObject:_quanpinDict[key]];
            }
        }
    }
    
    
    for (NSString *key in _jianpinDict) {
        
        if ([key.uppercaseString rangeOfString:string.uppercaseString].length != 0) {
            
            if (![cityModals containsObject:_jianpinDict[key]]) {
                [cityModals addObject:_jianpinDict[key]];
            }
        }
    }
    
    for (NSString *key in _totalCities) {
        
        if ([key rangeOfString:string].length != 0) {
            
            if (![cityModals containsObject:_totalCities[key]]) {
                [cityModals addObject:_totalCities[key]];
            }
        }
    }
    
    return cityModals;
    
}












@end
